package javase.ch05.homeWork.Demo2;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class Service {
    public static void main(String[] args) throws IOException {
        //客户端：将本地文件上传到服务器。接收服务器的反馈
        //服务器端：将接收到的文件保存到指定目录下，并返回上传成功

        //创建线程池对象
        ThreadPoolExecutor pool = new ThreadPoolExecutor(
                3,  //核心线程数
                16,     //最大线程数
                60,       //空闲时间
                TimeUnit.SECONDS, //时间单位
                new ArrayBlockingQueue<>(2), //任务队列
                Executors.defaultThreadFactory(), //线程工厂，让线程池如何创建线程对象
                new ThreadPoolExecutor.DiscardOldestPolicy() //拒绝策略
        );

        ServerSocket ss = new ServerSocket(10086);

        while (true) {
            Socket socket = ss.accept();

            //开启一条线程
            //一个用户对应服务端一个线程
           // new Thread(new ServiceRunnable(socket)).start();
            pool.submit(new MyRunnable(socket));
        }
    }
}
